<?php


namespace app\api\lists;


use app\common\enum\OrderEnum;
use app\common\model\order\Order;

class OrderLists extends BaseShopDataLists
{
    /**
     * @notes 搜索条件
     * @return array
     * @author ljj
     * @date 2022/2/28 9:31 上午
     */
    public function where()
    {
        $where = [];
        $where[] = ['user_id','=',$this->userId];
        if (isset($this->params['order_status']) && $this->params['order_status'] != '') {
            switch ($this->params['order_status']) {
                case 1:
                    $where[] = ['order_status','=',OrderEnum::ORDER_STATUS_WAIT_PAY];
                    break;
                case 2:
                    $where[] = ['order_status','=',OrderEnum::ORDER_STATUS_APPOINT];
                    break;
                case 3:
                    $where[] = ['order_status','=',OrderEnum::ORDER_STATUS_SERVICE];
                    break;
                case 4:
                    $where[] = ['order_status','=',OrderEnum::ORDER_STATUS_FINISH];
                    break;
                case 5:
                    $where[] = ['order_status','=',OrderEnum::ORDER_STATUS_CLOSE];
                    break;
            }
        }

        return $where;
    }

    /**
     * @notes 订单列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author ljj
     * @date 2022/2/28 9:31 上午
     */
    public function lists(): array
    {
        $lists = Order::field('id,sn,order_status,pay_status,order_amount,appoint_time_start,appoint_time_end,create_time,staff_id')
            ->order('id','desc')
            ->append(['appoint_time','appoint_week','order_status_desc','pay_btn','cancel_btn','del_btn','comment_btn','order_cancel_time','contact_btn'])
            ->with(['order_goods' => function($query){
                $query->field('order_id,goods_snap,goods_name')->append(['goods_image'])->hidden(['goods_snap']);
            },'staff' => function($query){
                $query->field('id,name,mobile,user_id');
            }])
            ->where($this->where())
            ->limit($this->limitOffset, $this->limitLength)
            ->select()
            ->toArray();

        return $lists;
    }

    /**
     * @notes 订单数量
     * @return int
     * @author ljj
     * @date 2022/2/28 9:32 上午
     */
    public function count(): int
    {
        return Order::where($this->where())->count();
    }
}